<?php
/**
 * User: i@fedax.cn
 * Time: 2018/8/24 13:24
 */

namespace app\admin\controller;

use app\admin\model\Vehicle as VehicleModel;

class Vehicle extends Index
{
    /**
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData()
    {
        $vehicleModel = new VehicleModel();
        $data = $vehicleModel->getDataList();
        return json($data);
    }

    /**
     * @return $this
     * @date 2019/1/4
     * @time 13:47
     * @user 帅帅吴
     * @title  查看座驾用户
     */
    public function vehicleUser()
    {
        $vehicleModel = new VehicleModel();
        $data = $vehicleModel->vehicleUserInfo();
        return view()->assign('data',$data);
    }

    /**
     * @return array
     * @date 2019/1/4
     * @time 14:29
     * @user 帅吴
     * @title 设置打折
     */
    public function edit()
    {
        $vehicleModel = new VehicleModel();
        $vehicle_id = input('get.vehicle_id');
        $is_discount = input('get.is_discount');
        $res = $vehicleModel->save(['is_discount'=>$is_discount],['vehicle_id'=>$vehicle_id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else  return ['code'=>-100,'msg'=>'修改失败'];
    }
    /**
     * @return array
     * @date 2019/1/4
     * @time 14:15
     * @user 帅帅吴
     * @title 非卖品上下架
     */
    public function vehicleChangeStatus()
    {
        $vehicleModel = new VehicleModel();
        $vehicle_id = input('get.vehicle_id');
        $is_putaway = $vehicleModel->where('vehicle_id','=',$vehicle_id)->value('is_putaway');
        $is_putaway = $is_putaway == 1?0:1;
        $res = $vehicleModel->save(['is_putaway'=>$is_putaway],['vehicle_id'=>$vehicle_id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else  return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function vehicleAddUser()
    {

        $u_id = input('get.u_id');
        $day = input('get.day');
        if($u_id){
            $user_id = \app\admin\model\User::where('u_id','=',$u_id)->value('id');
            if(empty($user_id)) return ['code'=>-100,'msg'=>'用户没有找到'];
            $newData['product_id'] = input('get.vehicle_id');
            $newData['user_id'] = $user_id;
            $newData['type'] = 1;
            $newData['status'] = 2;
            $newData['create_time'] = time();
            $newData['expire_time'] = time() + $day * 24 * 3600;

            $where[] = ['product_id','=',$newData['product_id']];
            $where[] = ['user_id','=',$user_id];
            $where[] = ['type','=',1];
            $where[] = ['status','=',2];
            $where[] = ['expire_time','>',time()];

            $res = \app\admin\model\ShopOrder::where($where)->find();

            if($res) $re = \app\admin\model\ShopOrder::where('shop_order_id', $res['shop_order_id'])->update(['expire_time' => $res['expire_time'] +$day * 24 * 3600 ]);
            else    $re =  \app\admin\model\ShopOrder::create($newData);

            if($re) return ['code'=>100,'msg'=>'添加成功'];
            else return ['code'=>-100,'msg'=>'添加失败'];

        }

        $vehicle_id = input('get.vehicle_id');
        return view()->assign('vehicle_id',$vehicle_id);
    }

    public function add()
    {
        $vehicleModel = new VehicleModel();
        $res = $vehicleModel->add();
        if($res['code']==100) $this->success($res['msg']);
        else $this->error($res['msg']);
    }
}